{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "01fca2b0-9126-4713-8d58-52243f62d85a",
   "metadata": {},
   "source": [
    "# 1、numpy的基础\n",
    "## 1.1、numpy的安装\n",
    "pip install numpy\n",
    "## 1.2、numpy的优势\n",
    "- 快（底层 C 实现）\n",
    "- 省（内存连续存储）\n",
    "- 简（向量化操作，无需循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "95c91a62-b26c-423d-a52b-1a1bb51e383b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lst:0.12487959861755371\n",
      "numpy:0.005000114440917969\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "\n",
    "lst = list(range(1000000))\n",
    "arr = np.arange(1000000)\n",
    "# print([x*2 for x in lst])\n",
    "# print(arr*2)\n",
    "t1 = time.time()\n",
    "[x*2 for x in lst] \n",
    "print(f'lst:{time.time()-t1}')\n",
    "t1 = time.time()\n",
    "arr*2\n",
    "print(f'numpy:{time.time()-t1}')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54dbcf05-f7a2-468a-838c-e7ef05edb87c",
   "metadata": {},
   "source": [
    "## 1.3、numpy的创建\n",
    "### 1.3.1、基于列表的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "58fd6aac-3689-44d4-917f-f2c45bfde44a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5] <class 'numpy.ndarray'>\n",
      "912\n",
      "[  1   3   5   7   9  11  13  15  17  19  21  23  25  27  29  31  33  35\n",
      "  37  39  41  43  45  47  49  51  53  55  57  59  61  63  65  67  69  71\n",
      "  73  75  77  79  81  83  85  87  89  91  93  95  97  99 101 103 105 107\n",
      " 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143\n",
      " 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179\n",
      " 181 183 185 187 189 191 193 195 197 199]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sys\n",
    "lst = [1,2,3,4,5]\n",
    "a1 = np.array(lst)\n",
    "print(a1,type(a1))\n",
    "a2 = np.array([1,2,3,4])\n",
    "a2\n",
    "#不建议使用这种方式\n",
    "a2 = np.array([range(1,10)])\n",
    "a2\n",
    "a3 = np.arange(1,200,2)\n",
    "#直接分配内存\n",
    "print(sys.getsizeof(a3))\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35f169a2-277e-4f35-844d-e2199e259910",
   "metadata": {},
   "source": [
    "### 1.3.2、其他创建方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "de9fdafd-9c4c-4618-af3a-689dec51dc83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0.   5.  10.  15.  20.  25.  30.  35.  40.  45.  50.  55.  60.  65.\n",
      "  70.  75.  80.  85.  90.  95. 100.] <class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'> [0. 0. 0. 0. 0.]\n",
      "[[0 0 0 0 0]\n",
      " [0 0 0 0 0]\n",
      " [0 0 0 0 0]\n",
      " [0 0 0 0 0]\n",
      " [0 0 0 0 0]]\n",
      "[[1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1.]]\n",
      "[[6 6 6 6 6]\n",
      " [6 6 6 6 6]\n",
      " [6 6 6 6 6]\n",
      " [6 6 6 6 6]\n",
      " [6 6 6 6 6]]\n"
     ]
    }
   ],
   "source": [
    "#利用np.linspace(start, stop, num=50, endpoint=True)可以创建等差数列\n",
    "'''\n",
    "假设我们要生成 num 个点：第一个点是 start,最后一个点是 stop,那么中间的间隔 = (stop - start) / (num - 1)\n",
    "'''\n",
    "a1 = np.linspace(0,100,21)\n",
    "print(a1,type(a1))\n",
    "#创建元素通过0填充\n",
    "a1 = np.zeros(5)\n",
    "print(type(a1),a1)\n",
    "#用0填充5行5列的二维数组，数据类型为int，默认情况是0.表示小数\n",
    "a2 = np.zeros((5,5),dtype=int)\n",
    "print(a2)\n",
    "#同理有np.ones,np.full,\n",
    "a1 = np.ones((4,5))\n",
    "a2 = np.full((5,5),6)\n",
    "print(a1)\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07202a85-2715-4cef-98a7-0311c9b6ff13",
   "metadata": {},
   "source": [
    "扩展讲解一下矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "48add0c5-2c50-4c65-b77d-09bf602437b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n",
      "[[0. 0. 1. 0. 0.]\n",
      " [0. 0. 0. 1. 0.]\n",
      " [0. 0. 0. 0. 1.]\n",
      " [0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.eye(3)\n",
    "print(a1)\n",
    "#k表示对角线的位置\n",
    "a1 = np.eye(5,k=2)\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "974b126c-21af-4293-95f0-aefb6d22f405",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.27308658 0.84423791 0.67231797 0.57405443 0.16044185 0.01412907\n",
      " 0.09798247 0.6422217  0.49526414]\n",
      "[[0.95150027 0.7513609  0.14833747 0.21520003 0.3749117 ]\n",
      " [0.5301235  0.38687076 0.98487147 0.72239622 0.91586972]\n",
      " [0.9172956  0.53301321 0.92786106 0.65480439 0.52018745]\n",
      " [0.9527162  0.79998938 0.97981154 0.94030441 0.30597845]\n",
      " [0.26384874 0.07128403 0.71892072 0.02053747 0.80489932]]\n",
      "[[8 3 4]\n",
      " [2 1 4]\n",
      " [1 7 1]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.random.rand(9)#随机生成9个浮点数\n",
    "print(a1)\n",
    "a2 = np.random.rand(5,5) #随机生成5行5列的浮点数\n",
    "print(a2)\n",
    "#随机生成3行3列的整数，范围在1，10之间\n",
    "a3 = np.random.randint(1,10,(3,3))\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "3d10254b-6893-454f-b9d5-3cc5c2248460",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.07338124 -1.32274044  0.04055489  0.54291012 -1.00950231  1.85156149\n",
      " -0.55766878  0.46340102 -0.70209314  0.61855961  2.43973917 -0.24646264\n",
      " -0.89662348  0.37789753 -0.43735507 -1.19230015 -0.54709495 -0.3091822\n",
      " -0.19450975 -0.56881228]\n",
      "[[ 1.90292949 -0.04879036 -0.06044122  1.24609438 -0.70076342]\n",
      " [-0.4158336  -1.01113577 -0.1022953   1.52249222 -0.46791862]\n",
      " [-1.36461147 -0.0743529  -0.78430841  2.14106288  1.29192446]\n",
      " [-1.66953198 -0.29673199  0.66847445 -0.92151529 -1.82978062]\n",
      " [-0.33524669  0.16120146  1.76327759  0.48212639  0.7266461 ]]\n",
      "[128.49111096 102.79750851 115.4622703  105.75167528  74.60778067\n",
      " 101.20184217  91.15903997 106.38267976 105.25811379 105.54804915\n",
      "  77.55391336  94.72791408 111.38556736 117.90330405  85.68838816\n",
      " 110.88947881  73.82825259  75.7145079  107.18803779 114.13238402]\n"
     ]
    }
   ],
   "source": [
    "#np.random.normal(loc=0.0, scale=1.0, size=None)\n",
    "a1 = np.random.normal(size=20) #均值为0，标准差为1\n",
    "print(a1)\n",
    "a2 = np.random.randn(5,5)#均值为0，标准差为1\n",
    "print(a2)\n",
    "a3 = np.random.normal(100,15,20)#均值为100，标准差为15，20个数\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "524bb5d1-994e-4a71-aed8-cfea81879416",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[   1.            2.15443469    4.64158883   10.           21.5443469\n",
      "   46.41588834  100.          215.443469    464.15888336 1000.        ]\n"
     ]
    }
   ],
   "source": [
    "#生成等比数列\n",
    "a1 = np.geomspace(1,1000,10)\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b64c562-9393-468b-ac15-7185246f183b",
   "metadata": {},
   "source": [
    "## 1.4、numpy的基本属性和方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "af66659c-4f74-4a93-ad5d-f817476ac520",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.79459036 0.04238974 0.35276473 0.2501561  0.94362907]\n",
      " [0.87629961 0.7270036  0.84842114 0.38200966 0.3212878 ]\n",
      " [0.51642884 0.63292149 0.02424398 0.03825336 0.58031148]]\n",
      "(3, 5)\n",
      "float64\n",
      "15\n",
      "2\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.rand(3,5)\n",
    "print(arr)\n",
    "#输出数组的形状\n",
    "print(arr.shape)\n",
    "#输出数据类型\n",
    "print(arr.dtype)\n",
    "#输出元素个数\n",
    "print(arr.size)\n",
    "#输出维度\n",
    "print(arr.ndim)\n",
    "#输出每个元素的大小\n",
    "print(arr.itemsize)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78160043-ca93-4b38-9957-99cde6d1ba39",
   "metadata": {},
   "source": [
    "## 1.5、基本的数据类型\n",
    "| 类型      | 精度     | 占字节          | 示例     |\n",
    "| ------- | ------ | ------------ | ------ |\n",
    "| int8    | 整数，1字节 | -128~127     | 灰度图像像素 |\n",
    "| int16   | 整数，2字节 | -32768~32767 | 音频信号   |\n",
    "| int32   | 整数，4字节 | 大多数整数运算      |        |\n",
    "| float32 | 小数，4字节 | GPU/深度学习常用   |        |\n",
    "| float64 | 小数，8字节 | 默认值（高精度）     |        |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "6608a830-1622-4ccb-a565-772f19d45df5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[62 19 93 48 65 45 97 92 20 41]\n",
      " [ 9 90 21 33 65 82  4 46 20 41]\n",
      " [14 11 95 68 37 13 88 15 76 97]\n",
      " [90 36 41 46 11 86 51 23 22 18]\n",
      " [ 9 45 52 65 40 15 73 18 75 48]\n",
      " [84 37 51 40 74 14  9 30 27 94]\n",
      " [24 56 65 73 68 10 72 28 34 31]\n",
      " [35 11 64 65 94 13  7 50 46 44]\n",
      " [79 50 62 76 20 16 71 55 96 57]\n",
      " [21 42 83 31 34 27 91 25 42 39]]\n",
      "int32 100 528\n",
      "****************************************************************************************************\n",
      "int16 100 328\n",
      "[[72 58 83 90 33 44 83  2  6 28]\n",
      " [67 41 28 66 19 86 40  5  9 37]\n",
      " [49 22 93  3 62  4 25 17 87  6]\n",
      " [35 58 42  7 42  7 56 25 71 92]\n",
      " [67 76 48 41 64 91 41 63  1 46]\n",
      " [25 47 76 69 13 52 73 91 81 79]\n",
      " [76 24 47 96 16 90 57 16 37  7]\n",
      " [91 12 43 10 36 80 23 15 23 31]\n",
      " [34 64 98 32 31 83 93 86 60 64]\n",
      " [99 42 79 43  7 34 23 37 30 49]]\n",
      "****************************************************************************************************\n",
      "int32 100 528\n",
      "[[72 58 83 90 33 44 83  2  6 28]\n",
      " [67 41 28 66 19 86 40  5  9 37]\n",
      " [49 22 93  3 62  4 25 17 87  6]\n",
      " [35 58 42  7 42  7 56 25 71 92]\n",
      " [67 76 48 41 64 91 41 63  1 46]\n",
      " [25 47 76 69 13 52 73 91 81 79]\n",
      " [76 24 47 96 16 90 57 16 37  7]\n",
      " [91 12 43 10 36 80 23 15 23 31]\n",
      " [34 64 98 32 31 83 93 86 60 64]\n",
      " [99 42 79 43  7 34 23 37 30 49]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a1 = np.random.randint(1,100,(10,10))\n",
    "print(a1)\n",
    "import sys\n",
    "print(a1.dtype,a1.size,sys.getsizeof(a1))\n",
    "print(\"*\"*100)\n",
    "a2 = np.random.randint(1,100,(10,10),dtype=np.int16)\n",
    "print(a2.dtype,a2.size,sys.getsizeof(a2))\n",
    "a3 = a2.astype(np.int32)\n",
    "print(a2)\n",
    "print(\"*\"*100)\n",
    "print(a3.dtype,a3.size,sys.getsizeof(a3))\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "701499f7-2905-4946-86a0-b19bc0e6d5c0",
   "metadata": {},
   "source": [
    "## 1.6、常用的查询方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7c719dd3-c8b5-4985-aa0a-82fdb58cce81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[62 33 36 27 73 62 80 52 92 35]\n",
      " [32 81 30 26 47 56 11 10 47 60]\n",
      " [26 43 86 51 74 59 39 76 87 46]\n",
      " [17 95 28 33  3 64 94 27 62 93]\n",
      " [33 33  1 81 58 54 88 37 78 17]\n",
      " [61 98 10 16 62 33 22  8 15 48]\n",
      " [37 16  5 68 63 30 97 79 70 91]\n",
      " [ 4 60 69 32 10 35 19 99 27 32]\n",
      " [59 77 44 89  4 42 17 87 14 59]\n",
      " [60 26 17 17 41  3 41 92 57 44]]\n",
      "****************************************************************************************************\n",
      "99 1\n",
      "55.2\n",
      "[27 33 35 36 52 62 62 73 80 92]\n",
      "[3 1 9 2 7 0 5 4 6 8]\n"
     ]
    }
   ],
   "source": [
    "#arr.max() arr.min()\n",
    "import numpy as np\n",
    "arr = np.random.randint(1,100,(10,10))\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#获取最大值和最小值\n",
    "print(arr.max(),arr.min())\n",
    "#获取第一行的平均值\n",
    "print(arr[0].mean())\n",
    "#排序\n",
    "arr1 = np.sort(arr[0])\n",
    "print(arr1)\n",
    "arr2 = np.argsort(arr[0])\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe0cbf5d-aa5d-40dc-81e0-ff34e180a832",
   "metadata": {},
   "source": [
    "# 2、numpy的操作\n",
    "## 2.1、切片\n",
    "### 2.1.1、切片基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "c35c3d3a-7a0c-45ff-b928-33fde911e625",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[1 2 3 4]\n",
      "[9 8 7 6 5 4 3 2 1 0]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(0,10)\n",
    "print(arr)\n",
    "print(arr[1:5])\n",
    "print(arr[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "829ccbf7-9c94-4578-8501-4472c72bb8d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[39 39 60  7 73 38]\n",
      " [13 80 85 71 26 14]\n",
      " [50 34 67 68 97 35]\n",
      " [61 82 91 86 50 74]\n",
      " [84 73 20 85 69  2]\n",
      " [88 19 81 39 69 54]]\n",
      "****************************************************************************************************\n",
      "[50 34 67 68 97 35]\n",
      "****************************************************************************************************\n",
      "[ 7 71 68 86 85 39]\n",
      "****************************************************************************************************\n",
      "[[ 7]\n",
      " [71]\n",
      " [68]\n",
      " [86]\n",
      " [85]\n",
      " [39]]\n",
      "****************************************************************************************************\n",
      "[[39 60  7]\n",
      " [34 67 68]\n",
      " [73 20 85]]\n",
      "****************************************************************************************************\n",
      "[[39]\n",
      " [13]\n",
      " [50]\n",
      " [61]\n",
      " [84]\n",
      " [88]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.randint(1,100,(6,6))\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#二维数组的切片,[行切片,列切片]\n",
    "print(arr[2])#取第二行\n",
    "print(\"*\"*100)\n",
    "print(arr[:,3])#取第三列,会变成一维数组，使用切片就不会降维\n",
    "print(\"*\"*100)\n",
    "print(arr[:,3:4])#取第三列，维持二维数组不变\n",
    "print(\"*\"*100)\n",
    "print(arr[::2,1:4])#偶数行，显示1->3的元素\n",
    "print(\"*\"*100)\n",
    "#讲解reshape方法,-1表示自动算行数，1表示有1列\n",
    "print(arr[:,0].reshape(-1,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0a0b1fe9-9a6d-4144-b430-c0afe248cfc9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(20).reshape(4,-1)\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0a1a7a2-830c-4779-bb54-5673d8973c78",
   "metadata": {},
   "source": [
    "### 2.1.2、切片练习\n",
    "- 创建一个1-20的连续数的4*5的二维数组\n",
    "- 取出第 2 行；\n",
    "- 取出第 3 列；\n",
    "- 取出前两行、后三列；\n",
    "- 取出右下角的 2×2 子矩阵；\n",
    "- 取出奇数行（第 1、3 行）；\n",
    "- 取出偶数列（第 0、2、4 列）；\n",
    "- 将矩阵的每一行倒序；\n",
    "- 将矩阵的行顺序完全倒序；\n",
    "- 取出前两行并将其展平成一维。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "dc9e135d-4c20-4520-b6fa-c3e3786e66f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]\n",
      " [11 12 13 14 15]\n",
      " [16 17 18 19 20]]\n",
      "****************************************************************************************************\n",
      "[ 6  7  8  9 10]\n",
      "[[ 3]\n",
      " [ 8]\n",
      " [13]\n",
      " [18]]\n",
      "[[ 3  4  5]\n",
      " [ 8  9 10]]\n",
      "[[14 15]\n",
      " [19 20]]\n",
      "[[ 6  7  8  9 10]\n",
      " [16 17 18 19 20]]\n",
      "[[ 1  3  5]\n",
      " [ 6  8 10]\n",
      " [11 13 15]\n",
      " [16 18 20]]\n",
      "[[ 5  4  3  2  1]\n",
      " [10  9  8  7  6]\n",
      " [15 14 13 12 11]\n",
      " [20 19 18 17 16]]\n",
      "[[16 17 18 19 20]\n",
      " [11 12 13 14 15]\n",
      " [ 6  7  8  9 10]\n",
      " [ 1  2  3  4  5]]\n",
      "[ 1  2  3  4  5  6  7  8  9 10]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#创建一个1-20的连续数的4*5的二维数组\n",
    "arr = np.arange(1,21).reshape(4,5)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#取出第 2 行；\n",
    "print(arr[1])\n",
    "#取出第 3 列；\n",
    "print(arr[:,2:3])\n",
    "#取出前两行、后三列；\n",
    "print(arr[0:2,2:])\n",
    "#取出右下角的 2×2 子矩阵；\n",
    "print(arr[-2:,-2:])\n",
    "#取出奇数行（第 1、3 行）；\n",
    "print(arr[1::2])\n",
    "#取出偶数列（第 0、2、4 列）；\n",
    "print(arr[:,::2])\n",
    "#将矩阵的每一行倒序；\n",
    "print(arr[:,::-1])\n",
    "#将矩阵的行顺序完全倒序；\n",
    "print(arr[::-1])\n",
    "#取出前两行并将其展平成一维。\n",
    "print(arr[0:2].reshape(-1))\n",
    "print(arr[0:2].reshape(1,-1))#平展成二维"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25ab4a16-b2b4-41dc-9a4e-e4ede320c62a",
   "metadata": {},
   "source": [
    "## 2.3、索引\n",
    "### 2.3.1、花式索引\n",
    "切片是连续的、花式索引可以不连续\n",
    "“我们之前用切片取的是连续区域：arr[2:6]。\n",
    "那如果我想取第 1、3、7 号元素呢？能做到吗？”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "95f1c305-0f64-4d5e-9947-e19e51c9cebc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9] <class 'numpy.ndarray'>\n",
      "[1 3 7]\n",
      "[2 5 1]\n",
      "****************************************************************************************************\n",
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]\n",
      " [20 21 22 23 24]]\n",
      "****************************************************************************************************\n",
      "[[ 5  6  7  8  9]\n",
      " [15 16 17 18 19]\n",
      " [20 21 22 23 24]]\n",
      "[[ 1  2  4]\n",
      " [ 6  7  9]\n",
      " [11 12 14]\n",
      " [16 17 19]\n",
      " [21 22 24]]\n",
      "****************************************************************************************************\n",
      "[ 1 14]\n",
      "****************************************************************************************************\n",
      "[ 9 23 11]\n",
      "****************************************************************************************************\n",
      "[[ 5  9  8  6]\n",
      " [20 24 23 21]\n",
      " [10 14 13 11]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(10)\n",
    "print(arr,type(arr))\n",
    "#中括号中加中括号\n",
    "print(arr[[1,3,7]])\n",
    "idx = [2,5,1]\n",
    "print(arr[idx])\n",
    "arr = np.arange(25).reshape(5,-10)\n",
    "print(\"*\"*100)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "print(arr[[1,3,4]])#取1，3，4行\n",
    "print(arr[:,[1,2,4]])#取1，2，4列\n",
    "print(\"*\"*100)\n",
    "#行和列配对取值\n",
    "#表示取第0行第1个元素，取第2行第四个元素\n",
    "print(arr[[0,2],[1,4]])\n",
    "print(\"*\"*100)\n",
    "#说一下如下结果,(1,4),(4,3),(2,1)\n",
    "print(arr[[1,4,2],[4,3,1]])\n",
    "#如果要取矩形块\n",
    "print(\"*\"*100)\n",
    "print(arr[np.ix_([1,4,2],[0,4,3,1])])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93a05787-79c0-4f24-a716-861be9f6ded4",
   "metadata": {},
   "source": [
    "### 2.3.2、布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "dce25dfb-e8b5-4cf3-b682-3e19d12d6abe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 5]\n",
      "[1 3]\n",
      "[False False  True  True  True]\n",
      "[14 27 23 20 53 45 56 96 65 13]\n",
      "[53 56 96 65]\n",
      "[14 20 56 13]\n",
      "[14 27 23 20 56 13]\n",
      "[20 45 56 96 65 13]\n",
      "[14 27 23 20 53 45]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1,2,3,4,5])\n",
    "ba = [True,True,False,False,True]\n",
    "print(arr[ba])#只有True的会显示\n",
    "print(arr[[True,False,True,False,False]])\n",
    "mask = arr>2\n",
    "#可以生成一个布尔索引数组\n",
    "print(mask)\n",
    "arr = np.random.randint(1,100,10)\n",
    "print(arr)\n",
    "#取出arr大于50的元素\n",
    "print(arr[arr>=50])\n",
    "idx = np.arange(10)\n",
    "print(arr[idx%3==0])\n",
    "#或运算\n",
    "print(arr[(idx%3==0)|(idx<3)])\n",
    "#与运算\n",
    "print(arr[(idx>2)&(idx!=4)])\n",
    "#非运算\n",
    "print(arr[~(idx>5)])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "849c480c-9615-411a-a0d1-764a72b2028e",
   "metadata": {},
   "source": [
    "### 2.3.3、索引练习\n",
    "- **基础题：**\n",
    "    - 取出第 1、3 行。  \n",
    "    - 取出第 0、2、3 行的第 2 列。  \n",
    "    - 取出第 0、2 行与第 1、3 列组成的矩形区域。  \n",
    "    - 取出 (0,1)、(1,3)、(3,0) 位置的元素。  \n",
    "    - 将第 1、3 行调换顺序输出。  \n",
    "***\n",
    "- **进阶题：**\n",
    "    - 取所有行的第 0、3 列。  \n",
    "    - 取第 0、2、3 行，且每行只取第 1、2 列。\n",
    "    - 倒序行输出上面的矩阵 \n",
    "    - 对第 0、3 行按列倒序输出。  \n",
    "    - 将数组的第 0、3 行互换位置（可一步实现）。  \n",
    "***\n",
    "\n",
    "  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5ac77204-a04e-4dc2-b38d-e50d6f6e7aea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "****************************************************************************************************\n",
      "[[ 5  6  7  8]\n",
      " [13 14 15 16]]\n",
      "[[ 3]\n",
      " [11]\n",
      " [15]]\n",
      "[[ 2  4]\n",
      " [10 12]]\n",
      "[ 2  8 13]\n",
      "[[ 9 10 11 12]\n",
      " [ 1  2  3  4]\n",
      " [13 14 15 16]\n",
      " [ 5  6  7  8]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(1,17).reshape(-1,4)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#取出第 1、3 行。\n",
    "print(arr[[1,3]])\n",
    "#取出第 0、2、3 行的第 2 列。\n",
    "print(arr[[0,2,3],[2]].reshape(-1,1))\n",
    "#取出第 0、2 行与第 1、3 列组成的矩形区域。\n",
    "print(arr[np.ix_([0,2],[1,3])])\n",
    "#取出 (0,1)、(1,3)、(3,0) 位置的元素。\n",
    "print(arr[[0,1,3],[1,3,0]])\n",
    "#将第 1、3 行调换顺序输出\n",
    "print(arr[[2,0,3,1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "25f809fb-6c98-41a3-85e7-6513f35a9df4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "****************************************************************************************************\n",
      "[[ 1  4]\n",
      " [ 5  8]\n",
      " [ 9 12]\n",
      " [13 16]]\n",
      "[[ 2  3]\n",
      " [10 11]\n",
      " [14 15]]\n",
      "[[ 3  2]\n",
      " [11 10]\n",
      " [15 14]]\n",
      "[[ 4  3  2  1]\n",
      " [16 15 14 13]]\n",
      "[[13 14 15 16]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [ 1  2  3  4]]\n"
     ]
    }
   ],
   "source": [
    "#取所有行的第 0、3 列。\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "print(arr[:,[0,3]])\n",
    "#取第 0、2、3 行，且每行只取第 1、2 列。\n",
    "print(arr[np.ix_([0,2,3],[1,2])])\n",
    "#倒序行输出上面的代码\n",
    "print(arr[np.ix_([0,2,3],[1,2])][:,::-1])\n",
    "#对第 0、3 行按列倒序输出。\n",
    "print(arr[[0,3],::-1])\n",
    "#将数组的第 0、3 行互换位置（可一步实现）。\n",
    "print(arr[[3,1,2,0]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fb8932e-0ee3-4f6b-bd45-a22c7cb245c0",
   "metadata": {},
   "source": [
    "### 2.3.4、布尔索引练习\n",
    "\n",
    "- **基础题：**\n",
    "    - 取出所有大于 10 的元素。  \n",
    "    - 取出所有偶数。  \n",
    "    - 取出所有小于 8 的元素。  \n",
    "    - 取出所有能被 3 整除的元素。  \n",
    "***\n",
    "- **进阶题：**\n",
    "    - 取出所有大于 5 且小于 12 的元素。  \n",
    "    - 取出所有奇数或能被 4 整除的数。  \n",
    "    - 取出所有不在 5 到 12 范围内的元素。  \n",
    "    - 取出前两行中所有偶数。  \n",
    "    - 取出第 0、2 行中大于 10 的元素。  \n",
    "    - 取出第 1～3 行中第 1、3 列的所有元素，并只保留能被 3 整除的数。  \n",
    "***\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "123ff7e8-129d-4380-8c11-0585da12459a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "****************************************************************************************************\n",
      "[11 12 13 14 15 16]\n",
      "[ 2  4  6  8 10 12 14 16]\n",
      "[1 2 3 4 5 6 7]\n",
      "[ 3  6  9 12 15]\n"
     ]
    }
   ],
   "source": [
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#取出所有大于 10 的元素。\n",
    "print(arr[arr>10])\n",
    "#取出所有偶数。\n",
    "print(arr[arr%2==0])\n",
    "#取出所有小于 8 的元素。\n",
    "print(arr[arr<8])\n",
    "#取出所有能被 3 整除的元素\n",
    "print(arr[arr%3==0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5f963d00-9c1d-4374-b33e-4440870914de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "****************************************************************************************************\n",
      "[ 6  7  8  9 10 11]\n",
      "[ 1  3  4  5  7  8  9 11 12 13 15 16]\n",
      "[ 1  2  3  4 13 14 15 16]\n",
      "[2 4 6 8]\n",
      "[11 12]\n",
      "****************************************************************************************************\n",
      "[ 6 12]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(1,17).reshape(-1,4)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#取出所有大于 5 且小于 12 的元素。\n",
    "print(arr[(arr>5)&(arr<12)])\n",
    "#取出所有奇数或能被 4 整除的数。\n",
    "print(arr[(arr%2==1)|(arr%4==0)])\n",
    "#取出所有不在 5 到 12 范围内的元素。\n",
    "print(arr[~((arr>=5)&(arr<=12))])\n",
    "#取出前两行中所有偶数。\n",
    "print(arr[:2][arr[:2]%2==0])\n",
    "#取出第 0、2 行中大于 10 的元素。\n",
    "print(arr[[0,2]][arr[[0,2]]>10])\n",
    "#取出第 1～3 行中第 1、3 列的所有元素，并只保留能被 3 整除的数。\n",
    "print(\"*\"*100)\n",
    "tarr = arr[1:4][:,[1,3]]\n",
    "print(tarr[tarr%3==0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27982a89-7737-4267-bae8-3213f862160f",
   "metadata": {},
   "source": [
    "### 2.3.5、混合练习（花式 + 布尔）\n",
    "\n",
    "- **综合题：**\n",
    "    - 取第 1、3 行中所有大于 8 的元素。\n",
    "    - 取第 0、3 行中大于 5 的元素\n",
    "    - 取第 1、2 行中能被 4 整除的数。 \n",
    "    - 取第 0、2 行的第 1、3 列，再筛选出其中大于 5 的元素。\n",
    "    - 取出第 1～3 行、第 2～5 列中所有大于 10 且为偶数的元素。\n",
    "    - 取出第 0、2、3 行中列索引为 [1,3,4] 的所有元素，再筛选能被 3 整除的数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "6f3cb74f-9d0e-4e1e-ad70-7ade74b3dc2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5  6]\n",
      " [ 7  8  9 10 11 12]\n",
      " [13 14 15 16 17 18]\n",
      " [19 20 21 22 23 24]]\n",
      "****************************************************************************************************\n",
      "[ 9 10 11 12 19 20 21 22 23 24]\n",
      "[ 6 19 20 21 22 23 24]\n",
      "[ 8 12 16]\n",
      "[14 16]\n",
      "[15 16 21 22]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(1,25).reshape(-1,6)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#取第 1、3 行中所有大于 8 的元素。\n",
    "tarr = arr[[1,3]]\n",
    "print(tarr[tarr>8])\n",
    "#取第 0、3 行中大于 5 的元素\n",
    "print(arr[[0,3]][arr[[0,3]]>5])\n",
    "#取第 1、2 行中能被 4 整除的数。\n",
    "print(arr[1:3][arr[1:3]%4==0])\n",
    "#取第 0、2 行的第 1、3 列，再筛选出其中大于 5 的元素\n",
    "blk = arr[np.ix_([0,2],[1,3])]\n",
    "print(blk[blk>5])\n",
    "#取出第 1～3 行、第 2～5 列中所有大于 10 且为偶数的元素。\n",
    "tarr = arr[1:4,2:4]\n",
    "print(tarr[tarr>10])\n",
    "#取出第 0、2、3 行中列索引为 [1,3,4] 的所有元素，再筛选能被 3 整除的数。\n",
    "tarr = arr[np.ix_([0,2,3],[1,3,4])]\n",
    "print(tarr[tarr%3==0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05163d01-29c5-41af-a200-18bf4ee91e23",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
